package vn.com.bookstore.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.bookstore.dao.AudioBookDao;
import vn.com.bookstore.domain.model.AudioBook;
import vn.com.bookstore.domain.model.PagingInfo;
import vn.com.bookstore.domain.model.SearchResult;
import vn.com.bookstore.utils.DaoHelper;

@Repository
public class AudioBookDaoImpl extends DaoHelper<AudioBook> implements
		AudioBookDao {

	@SuppressWarnings("rawtypes")
	class AudioBookRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			AudioBook book = new AudioBook();
			book.setId(rs.getInt("id"));
			book.setTitle(rs.getString("title"));
			book.setPrice(rs.getDouble("price"));
			book.setDiscount(rs.getDouble("discount"));
			book.setDescription(rs.getString("description"));
			book.setUrlAudio(rs.getString("url_audio"));
			book.setImageUrl(rs.getString("image_url"));
			try {
				book.setPublisherId(rs.getInt("publisher_id"));
			} catch (Exception e) {
			}

			try {
				book.setActive(rs.getInt("active"));
			} catch (Exception e) {
			}
			try {
				book.setCreatedDate(rs.getDate("create_date"));
			} catch (Exception e) {
			}

			return book;
		}

	}


	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getAudioBookWithKeyWord(String keyword, PagingInfo pagingInfo) {
		StringBuilder sql = new StringBuilder("Select ab.id, ab.title, ab.description, ab.url_audio, ab.created_date, ab.price, ");
		sql.append(" ab.discount, ab.publisher_id, ab.image_url, ab.active ")
		.append(" from audio_book ab inner join publisher p on ab.publisher_id = p.id WHERE p.active = 1 AND ab.active = 1 ");
		if(keyword != null && keyword.length() > 0) {
			sql.append(" AND (");
			String[] keyStrings = keyword.split(" ");
			for (int i = 0; i < keyStrings.length; i++) {
				String s = "'%" + keyStrings[i] + "%'";
				if (i == 0) {
					sql.append(" ab.title LIKE ").append(s).append(" OR ab.description LIKE ").append(s)
					.append(" OR p.name LIKE ").append(s);
				} else {
					sql.append(" OR ab.title LIKE ").append(s).append(" OR ab.description LIKE ").append(s)
					.append(" OR p.name LIKE ").append(s);
					
				}
			}
			sql.append(")");
		}
		return queryPaging(sql.toString(), null, new AudioBookRowMapper(), pagingInfo);
	}


	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getAudioBookByid(int id) {
		String sql = "Select * from audio_book where id = ?";
		Object[] params = new Object[]{id};
		return query(sql, params, new AudioBookRowMapper());
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getListAudioBookByUsername(int publisherId,
			PagingInfo pagingInfo, String keyword) {
		StringBuilder sql = new StringBuilder("SELECT * FROM audio_book WHERE publisher_id = ? ");
		if(keyword != null && keyword.length() > 0) {
			sql.append(" AND title LIKE '%").append(keyword).append("%'");
		}
		Object[] params = new Object[]{publisherId};
		return queryPaging(sql.toString(), params, new AudioBookRowMapper(), pagingInfo);
	}


	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getAudioBooks(PagingInfo pagingInfo) {
		String sql = "Select * from audio_book WHERE active = 1";
		return queryPaging(sql, null, new AudioBookRowMapper(), pagingInfo);
	}
	
}
